
####### ----------------------------------------------------------------------
## 1. Summary Statistics
####### ----------------------------------------------------------------------
####### ----------------------------------------------------------------------

modeldata <- final.data.dyad%>%
  mutate(coopTotal=log(coopTotal + logdelta),
         hostTotal=log(abs(hostTotal) + logdelta),
         mindist=log(mindist + 1),
         wdi_pop.x=log(wdi_pop.x),
         wdi_pop.y=log(wdi_pop.y),
         wdi_gdppppcon2011.x=log(wdi_gdppppcon2011.x),
         wdi_gdppppcon2011.y=log(wdi_gdppppcon2011.y)) %>%
  dplyr::select(c("coopTotal","hostTotal","KOFEcGIdf.x", "KOFEcGIdf.y", 
         "major.sym", "major.asym1", "major.asym2",
         "dep1","dep2","dyadic.balance",
         "caprat", "mindist", "wdi_pop.x", "wdi_pop.y",
         "wdi_gdppppcon2011.x", "wdi_gdppppcon2011.y",
         "dem1", "dem2")) %>% na.omit()

sumstat <- stat.desc(modeldata)

names(sumstat) <- c("log Cooperation","log Confict","KOF 1","KOF 2",
                    "Mutual Major Trade Partners", "1 Is Major Partner of 2", "2 Is Major Partner of 1", 
                    "Trade/GDP 1", "Trade/GDP 2", "Trade Balance",
                    "CINC Ratio", "log Distance", "log Population 1","log Population 2", 
                    "log GDP 1", "log GDP 2",
                    "Democracy 1", "Democracy 2")

sumstat <- as.data.frame(t(sumstat)) %>% 
  dplyr::select("median","mean", "std.dev", "min", "max") %>%
  round(2)

dir.create("AppendixMaterials/SummaryStat")

stargazer(sumstat, title="Summary Statisics", label="sumstat", 
          digits=2, font.size = "scriptsize",
          notes=paste0("Total number of observation: ", nrow(modeldata)),
          summary=FALSE, out="AppendixMaterials/SummaryStat/sumstat.tex")


## 2. To test the diagonality assumption
## DY LDV
coop.f.d.l <- log(post.coopTotal + logdelta)  ~  log(coopTotal + logdelta) + log(abs(hostTotal) + logdelta) +
  (KOFEcGIdf.x.0 + KOFEcGIdf.y.0) * (major.sym + major.asym1 + major.asym2) + 
  dep1 * dep2 + dyadic.balance + caprat + log(mindist + 1) + log(wdi_pop.x) + log(wdi_pop.y) +
  log(wdi_gdpcappppcon2011.x) + log(wdi_gdpcappppcon2011.y) + dem1 * dem2

conf.f.d.l <- log(abs(post.hostTotal) + logdelta)  ~ log(coopTotal + logdelta) + log(abs(hostTotal) + logdelta) + 
  (KOFEcGIdf.x.0 + KOFEcGIdf.y.0) * (major.sym + major.asym1 + major.asym2) + 
  dep1 * dep2 + dyadic.balance + caprat + log(mindist + 1) + log(wdi_pop.x) + log(wdi_pop.y) +
  log(wdi_gdpcappppcon2011.x) + log(wdi_gdpcappppcon2011.y) + dem1 * dem2

fitsur.f.d.l <- systemfit(list(coop = coop.f.d.l, conf = conf.f.d.l), data=final.data.dyad, method = "SUR")
summary(fitsur.f.d.l)
fitols.f.d.l <- systemfit(list(coop = coop.f.d.l, conf = conf.f.d.l), data=final.data.dyad, method = "OLS")
lrtest(fitols.f.d.l,fitsur.f.d.l)